package com.transaction.simulate.service.impl;

import com.transaction.common.util.CodeUtil;
import com.transaction.simulate.mapper.MenuMapper;
import com.transaction.simulate.mapper.RoleMapper;
import com.transaction.simulate.mapper.UserMapper;
import com.transaction.simulate.model.dto.Role;
import com.transaction.simulate.model.dto.RoleMenu;
import com.transaction.simulate.model.vo.RoleMenuVo;
import com.transaction.simulate.service.RoleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addRole(Role role) {
        role.setRoleCode(CodeUtil.getCode());
        roleMapper.addRole(role);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public RoleMenuVo authorizationRole(String roleCode) {
        //查询所有菜单
        List<RoleMenu> allMenu = menuMapper.selectAllMenu();
        List<RoleMenu> roleMenuList = buildMenus(allMenu);

        //查询已赋权的菜单
        List<String> authorizationMenuCodes = menuMapper.selectMenusByRoleCode(roleCode);

        return new RoleMenuVo(roleMenuList,authorizationMenuCodes);
    }

    private List<RoleMenu> buildMenus(List<RoleMenu> menuList) {

        //组装一级菜单
        List<RoleMenu> menus = menuList.stream().filter(s -> Objects.isNull(s.getParentId())).collect(Collectors.toList());
        //在一级菜单基础上组装二级菜单
        menus.forEach(p->{
            String menuCode = p.getId();
            List<RoleMenu> children = new ArrayList<>();
            menuList.forEach(c->{
                if(Objects.equals(menuCode,c.getParentId())){
                    children.add(c);
                }
            });
            p.setChildren(children);
        });

        return menus;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteRole(String roleCode) {
        //删除角色
        roleMapper.deleteRole(roleCode);
        //删除角色菜单
        menuMapper.deleteRoleMenuByRoleCode(roleCode);
        //删除用户角色
        userMapper.deleteUserRoleBuRoleCode(roleCode);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addRoleMenu(RoleMenuVo roleMenuVo){
        String roleCode = roleMenuVo.getRoleCode();
        List<String> checkedMenuCodes = roleMenuVo.getCheckedMenuCodes();
        if(StringUtils.isNotEmpty(roleCode)){
            //删除已有角色的菜单
            menuMapper.deleteRoleMenuByRoleCode(roleCode);
        }else{
            throw new RuntimeException("请选择角色");
        }
        if(CollectionUtils.isNotEmpty(checkedMenuCodes)) {
            //插入现有的角色菜单
            roleMapper.insertRoleMenus(roleCode, checkedMenuCodes);
        }

    }
}
